<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button icon="el-icon-plus" size="small" type="primary" @click="isShowAddBox = true">新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <!--  type="index" 表示列的内容自增 -->
              <el-table-column label="序号" width="100" type="index" :index="handleIndex" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="handleEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination :page-sizes="[2, 4, 6, 8, 10, 12]" layout="sizes, prev, pager, next, jumper, ->, total" :total="total" :current-page="queryParams.page" :page-size="queryParams.pagesize" @current-change="changePage"
              @size-change="sizeChange" />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="isShowAddBox" @close="aaa">
      <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入角色描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="handleAdd">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetRoleList, reqDeleteRole, reqAddRole, reqGetRoleDetail, reqUpdateRole } from '@/api/setting.js'

export default {
  name: 'Setting',
  data () {
    return {
      isShowAddBox: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ]
      },
      loading: false,
      // 总条数
      total: 0,
      // 角色列表
      list: [],
      // 查询参数
      queryParams: {
        page: 1,
        pagesize: 2
      }
    }
  },
  computed: {
    title () {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created () {
    this.loadRoleList()
  },
  methods: {
    // 编辑显示弹窗
    async handleEdit (id) {
      try {
        const ret = await reqGetRoleDetail(id)
        if (ret.code === 10000) {
          this.form = ret.data
          this.isShowAddBox = true
        }
      } catch {
        this.$message.error('获取角色信息失败')
      }
    },
    // 关闭弹窗,清空表单，
    aaa () {
      this.$refs.roleForm.resetFields()
    },
    // 取消
    btnCancel () {
      // 关闭弹窗,清空表单，刷新列表数据,
      this.isShowAddBox = false
      this.$refs.roleForm.resetFields()
      this.loadRoleList()
    },
    // 添加角色
    handleAdd () {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          // 编辑角色
          try {
            const ret = await reqUpdateRole(this.form)
            if (ret.code === 10000) {
              this.loadRoleList()
              this.isShowAddBox = false
            }
          } catch {
            this.$message.erroe('编辑角色失败')
          }
        } else {
          // 添加角色
          try {
            const ret = await reqAddRole(this.form)
            if (ret.code === 10000) {
              // 刷新表单，关闭弹窗，清空列表
              this.loadRoleList()
              this.isShowAddBox = false
              this.$refs.roleForm.resetFields()
              this.$message.success('添加成功')
            }
          } catch {
            this.$message.error('添加失败')
          }
        }
      })
    },

    // 删除角色
    async handleDel (id) {
      try {
        const ret = await this.$confirm('确定删除角色？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        // 点击了取消
        if (ret === 'cancel') return
        // 点击了确定
        const delRet = await reqDeleteRole(id)
        if (delRet.code === 10000) {
          // 删除成功，刷新列表
          this.loadRoleList()
          this.$message.success('删除成功')
          const num = Math.ceil((this.total - 1) / this.queryParams.pagesize)
          this.queryParams.page = this.queryParams.page > num ? num : this.queryParams.page
        } else {
          this.$message.success(delRet.message)
        }
      } catch {
        this.$message.error('删除失败')
      }
    },
    // 动态生成索引
    handleIndex (index) {
      const { page, pagesize } = this.queryParams
      return (page - 1) * pagesize + index + 1
    },
    // 监听每页条数的变化
    sizeChange (pagesize) {
      this.queryParams.pagesize = pagesize
      this.loadRoleList()
    },
    // 监听页码的切换操作
    changePage (page) {
      // page表示切换后最新的页码
      this.queryParams.page = page
      this.loadRoleList()
    },
    // 获取角色列表数据
    async loadRoleList () {
      try {
        this.loading = true
        const ret = await reqGetRoleList(this.queryParams)
        if (ret.code === 10000) {
          this.list = ret.data.rows
          this.total = ret.data.total
        }
      } catch {
        this.$message.error('获取角色列表失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
